package com.example.lotterysystem.dao.handler;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.example.lotterysystem.dao.dataobject.Encrypt;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;


@MappedTypes(Encrypt.class)             //被处理的类型
@MappedJdbcTypes(JdbcType.VARCHAR)      //转换后的jdbc类型
public class EncryptTypeHandler extends BaseTypeHandler<Encrypt> {

    private final byte[] KEY = "123456789abcdefg".getBytes(StandardCharsets.UTF_8);
    /**
     * ps sql预编译对象
     * i    需要赋值的索引位置
     * parameter    原本位置i需要赋的值
     * jdbctype     jdbc类型
     * */
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Encrypt parameter, JdbcType jdbcType) throws SQLException {
        if (parameter == null || parameter.getValue() == null)
        {
            ps.setString(1,null);
            return;
        }
        System.out.println("加密的内容："+parameter.getValue());
        //加密
        AES aes = SecureUtil.aes(KEY);
        String str = aes.encryptHex(parameter.getValue());
        ps.setString(i,str);
    }


    /**
     * 获得值的加密过程
     * */
    @Override
    public Encrypt getNullableResult(ResultSet rs, String columnName) throws SQLException {
        System.out.println("获取值的加密内容："+rs.getString(columnName));
        return decrypt(rs.getString(columnName));
    }


    /**
     * rs 结果集合
     * columnIndex 索引位置
     * */
    @Override
    public Encrypt getNullableResult(ResultSet rs, int columnIndex) throws SQLException {

        System.out.println("获取值的加密内容："+rs.getString(columnIndex));
        return decrypt(rs.getString(columnIndex));
    }


    //解密
    private Encrypt decrypt(String str)
    {
        if(!StringUtils.hasText(str))
        {
            return null;
        }
        return new Encrypt(SecureUtil.aes(KEY).decryptStr(str));
    }


    @Override
    public Encrypt getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return null;
    }
}
